package com.fenghongzhang.day007;

import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.core.content.ContextCompat;

public class MyView extends RelativeLayout {

    private Drawable[] drawables = new Drawable[5];

    private int width;
    private int height;

    private ImageView imageView;
    public MyView(Context context) {
        super(context);
        init();
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        drawables[0] = ContextCompat.getDrawable(getContext(),R.mipmap.ic_launcher);
        drawables[1] = ContextCompat.getDrawable(getContext(),R.mipmap.ic_launcher);
        drawables[2] = ContextCompat.getDrawable(getContext(),R.mipmap.ic_launcher);
        drawables[3] = ContextCompat.getDrawable(getContext(),R.mipmap.ic_launcher);
        drawables[4] = ContextCompat.getDrawable(getContext(),R.mipmap.ic_launcher);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
    }

    public void addImageView(){
        imageView =  new ImageView(getContext());
        imageView.setImageDrawable(drawables[(int)(Math.random()*drawables.length)]);//0-4
        addView(imageView); //添加

        //来 一个动画
        startBezierAnimoator(imageView).start();

    }


    private ValueAnimator startBezierAnimoator(final ImageView imageView) {

        MyEvaluator myEvaluator = new MyEvaluator(getFointF(),getFointF());
        //起始点,和结束点
        ValueAnimator valueAnimator = ValueAnimator.ofObject(myEvaluator, new PointF(width / 2, height), new PointF((float) Math.random() * width, (float) Math.random() * 50));

        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                PointF pointF = (PointF) animation.getAnimatedValue();
                imageView.setX(pointF.x);
                imageView.setY(pointF.y);
                imageView.setAlpha(1-animation.getAnimatedFraction());
            }
        });
        valueAnimator.setTarget(imageView);
        valueAnimator.setDuration(3000);
        return valueAnimator;
    }

    public void removeImageView(){
        removeAllViews();
    }


    public PointF getFointF(){
        PointF pointF = new PointF();
        pointF.x =(float) Math.random()*width;
        pointF.y =(float) Math.random()*height/4;
        return pointF;
    }

    //3阶贝塞尔曲线
    class MyEvaluator implements TypeEvaluator<PointF>{

        private PointF pointF1;
        private PointF pointF2;

        public MyEvaluator(PointF pointF1, PointF pointF2) {
            this.pointF1 = pointF1;
            this.pointF2 = pointF2;
        }

        @Override
        public PointF evaluate(float time, PointF startValue, PointF endValue) {
            float timeLeft = 1 -time;
            PointF point = new PointF();//结果
            point.x = timeLeft * timeLeft * timeLeft * (startValue.x)
                    + 3 * timeLeft * timeLeft * time * (pointF1.x)
                    + 3 * timeLeft * time * time * (pointF2.x)
                    + time * time * time * (endValue.x);

            point.y = timeLeft * timeLeft * timeLeft * (startValue.y)
                    + 3 * timeLeft * timeLeft * time * (pointF1.y)
                    + 3 * timeLeft * time * time * (pointF2.y)
                    + time * time * time * (endValue.y);
            return point;
        }
    }
}
